package com.lhf.novel.sysuser.controller;

import com.github.pagehelper.PageInfo;
import com.lhf.novel.sysuser.entity.SysPer;
import com.lhf.novel.sysuser.service.SysPerService;
import com.lhf.novel.sysuser.vo.SysPerListVo;
import com.lhf.novel.sysuser.vo.SysPerTreeVo;
import com.lhf.novel.util.Result;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SysPer)表控制层
 *
 * @author 刘会发
 * @since 2020-03-09 11:21:33
 */
@RestController
@RequestMapping("sysPer")
@Api(tags = "权限控制层")
public class SysPerController {
    /**
     * 服务对象
     */
    @Resource
    private SysPerService sysPerService;


    @PostMapping("add")
    @ApiOperation("添加权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "perName", value = "权限名字", dataTypeClass = String.class, example = "系统管理", required = true),
            @ApiImplicitParam(name = "per", value = "权限", dataTypeClass = String.class, example = "PER_SYS", required = true),
            @ApiImplicitParam(name = "perIco", value = "图标", dataTypeClass = String.class, example = "el-icon-user"),
            @ApiImplicitParam(name = "compoment", value = "组件", dataTypeClass = String.class, example = "System"),
            @ApiImplicitParam(name = "path", value = "跳转路径", dataTypeClass = String.class, example = "/system"),
            @ApiImplicitParam(name = "type", value = "权限类型 0菜单 1按钮", dataTypeClass = Integer.class, example = "0", required = true),
            @ApiImplicitParam(name = "parentId", value = "父类id 0是一级菜单", dataTypeClass = Integer.class, example = "0", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权访问")
    })
    public Result<SysPer> insert(String perName,
                                 String per,
                                 @RequestParam(required = false) String perIco,
                                 @RequestParam(required = false) String compoment,
                                 @RequestParam(required = false) String path,
                                 Integer type,
                                 Integer parentId) {
        return Result.success(sysPerService.insert(new SysPer(perName, per, perIco, compoment, path, type, parentId)));
    }

    @DeleteMapping("delete")
    @ApiOperation("删除权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "权限id", dataTypeClass = Integer.class, example = "1"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权访问")
    })
    public Result<SysPer> delete(Integer pid) throws IllegalAccessException {
        return Result.success(sysPerService.update(new SysPer(pid, 1)));
    }

    @PutMapping("update")
    @ApiOperation("修改权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "perName", value = "权限名字", dataTypeClass = String.class, example = "系统管理"),
            @ApiImplicitParam(name = "per", value = "权限", dataTypeClass = String.class, example = "PER_SYS"),
            @ApiImplicitParam(name = "perIco", value = "图标", dataTypeClass = String.class, example = "el-icon-user"),
            @ApiImplicitParam(name = "compoment", value = "组件", dataTypeClass = String.class, example = "System"),
            @ApiImplicitParam(name = "path", value = "跳转路径", dataTypeClass = String.class, example = "/system"),
            @ApiImplicitParam(name = "type", value = "权限类型 0菜单 1按钮", dataTypeClass = Integer.class, example = "0"),
            @ApiImplicitParam(name = "parentId", value = "父类id 0是一级菜单", dataTypeClass = Integer.class, example = "0"),
            @ApiImplicitParam(name = "pid", value = "主键，标识", dataTypeClass = Integer.class, example = "1", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权访问")
    })
    public Result<SysPer> update(@RequestParam(required = false) String perName,
                                 @RequestParam(required = false) String per,
                                 @RequestParam(required = false) String perIco,
                                 @RequestParam(required = false) String compoment,
                                 @RequestParam(required = false) String path,
                                 @RequestParam(required = false) Integer type,
                                 @RequestParam(required = false) Integer parentId,
                                 Integer pid) throws IllegalAccessException {
        return Result.success(sysPerService.update(new SysPer(pid, perName, per, perIco, compoment, path, type, parentId, null)));
    }


    @GetMapping("queryAll")
    @ApiOperation("根据条件查询权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "perName", value = "权限列表", dataTypeClass = String.class, example = "系统管理"),
            @ApiImplicitParam(name = "per", value = "权限", dataTypeClass = String.class, example = "PER_SYS"),
            @ApiImplicitParam(name = "parentId", value = "父类id", dataTypeClass = String.class, example = "0", defaultValue = "0"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataTypeClass = Integer.class, example = "1", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示数量", dataTypeClass = Integer.class, example = "10"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权访问")
    })
    public Result<PageInfo<SysPerListVo>> queryAll(@RequestParam(required = false) String perName,
                                                   @RequestParam(required = false) String per,
                                                   @RequestParam(required = false, defaultValue = "0") Integer parentId,
                                                   @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(sysPerService.queryAll(perName, per, parentId, pageNum, pageSize));
    }

    @GetMapping("selectOne")
    @ApiOperation("根据id查询权限的详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "权限id", dataTypeClass = String.class, example = "1"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权访问")
    })
    public Result<SysPer> selectOne(Integer id) {
        return Result.success(sysPerService.queryById(id));
    }

    @GetMapping("tree")
    @ApiOperation("权限树状结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父类id", dataTypeClass = Integer.class, example = "1", defaultValue = "0"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常")
    })
    public Result<List<SysPerTreeVo>> listPerTree(@RequestParam(required = false, defaultValue = "0") Integer parentId) {
        return Result.success(sysPerService.listPerTree(parentId));
    }
}